#!/usr/bin/env python
'''Run Google Query'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
__license__ = "BSD" # see LICENSE

import webbrowser

WEB_URL = "http://www.google.com/search?esrch=BetaShortcuts&q=%s"
IMAGE_URL = "http://images.google.com/images?q=%s"

def google(query, url=WEB_URL):
    if isinstance(query, (tuple, list)):
        query = "+".join((item.strip() for item in query if item))

    url = url % query
    webbrowser.open(url)

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from optparse import OptionParser

    parser = OptionParser("usage: %prog QUERY [QUERY2 ...]")
    parser.add_option("-i", "--images", help="search images",
        dest="images", action="store_true", default=0)

    opts, args = parser.parse_args(argv[1:])
    if len(args) < 1:
        parser.error("wrong number of arguments") # Will exit

    url = IMAGE_URL if opts.images else WEB_URL
    google(args, url)

if __name__ == "__main__":
    main()
